package com.fcx.workai.loginmodule.ui.smslogin;
/**
 * Created by hxh on 2017/8/4.
 */

import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import com.fcx.workai.loginmodule.R;
import com.fcx.workai.loginmodule.ui.login.PwdLoginFragment;
import com.hxh.component.basicore.component.mvp.view.BaseFragment;
import com.hxh.component.business.arouter.MyAppProvider;
import com.hxh.component.business.bean.OpenProgressBean;
import com.hxh.component.business.common.PdfViewerActivity;
import com.hxh.component.business.common.UserInfoBean;
import com.hxh.component.business.common.api.Request_GetVerityCode;
import com.hxh.component.business.ui.MyUtil;
import com.hxh.component.business.utils.Constant;


/**
 * 创建者：hxh
 * 时间：  2017/8/4
 * 描述：
 */
public class SMSLoginFragment extends BaseFragment<SMSLoginPresenter> implements SMSLoginContract.V {
    private EditText et_acount, et_veritycode;
    private Button btn_login;
    private TextView tv_getveritycode, tv_pwdlogin,tv_jump_over,btn_registrationagreement,btn_yinsiagreement;
    private CheckBox cb_registe;

    public static SMSLoginFragment newInstance() {

        Bundle args = new Bundle();

        SMSLoginFragment fragment = new SMSLoginFragment();
        fragment.setArguments(args);
        return fragment;
    }

    public static SMSLoginFragment newInstance(String acount) {

        Bundle args = new Bundle();
        args.putString("acount", acount);
        SMSLoginFragment fragment = new SMSLoginFragment();
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public int getLayoutId() {
        return R.layout.fragment_smslogin;
    }

    @Override
    public void initData(Bundle saveInstanceState) {
        initView();
    }

    private void initView() {
        OnClickListener lis = new OnClickListener();
        et_acount = findViewBy(R.id.et_acount);
        et_veritycode = findViewBy(R.id.et_veritycode);
        btn_login = findViewBy(R.id.btn_login);
        tv_getveritycode = findViewBy(R.id.tv_getveritycode);
        tv_pwdlogin = findViewBy(R.id.tv_pwdlogin);
        tv_jump_over = findViewBy(R.id.tv_jump_over);
        btn_registrationagreement = findViewBy(R.id.btn_registrationagreement);
        btn_yinsiagreement = findViewBy(R.id.btn_yinsiagreement);
        cb_registe = findViewBy(R.id.cb_registe);
        cb_registe.setChecked(true);
        String acount = getExtra_String("acount");

        tv_getveritycode.setOnClickListener(lis);
        btn_login.setOnClickListener(lis);
        tv_pwdlogin.setOnClickListener(lis);
        tv_jump_over.setOnClickListener(lis);
        btn_registrationagreement.setOnClickListener(lis);
        btn_yinsiagreement.setOnClickListener(lis);
        if (!isEmpty(acount)) setText(et_acount, acount);

        //TODO 方便测试切换登录使用
        findViewBy(R.id.iv_icon).setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                startWithPop(PwdLoginFragment.newInstance(et_acount.getText().toString()));
                return false;
            }
        });
    }

    class OnClickListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {

            if (v.getId() == R.id.btn_login) {

                if (!isEmpty(et_acount, "请输入手机号") && !MyUtil.isMobile(getText(et_acount))){
                    toastShort("请输入有效的手机号码");
                    return;
                }

                if (!cb_registe.isChecked()){
                    toastShort("请您接受并同意用户协议");
                    return;
                }

                if (!isEmpty(et_acount, "请输入手机号") && !isEmpty(et_veritycode, "请输入验证码")) {
                    if (MyUtil.isMobile(getText(et_acount))) {
                        final String mTenantId;
                        String baseUrl = com.hxh.component.business.BuildConfig.AUTH_ENDPOINT;
                        if ("https://api.workai.com.cn/".equals(baseUrl) || "http://120.55.17.174/".equals(baseUrl)) {
                            mTenantId = Constant.TENANT_ID;
                        } else {
                            mTenantId = Constant.DEBUF_TENANT_ID;
                        }
                        getP().login(getText(et_acount), getText(et_veritycode), Constant.LOGINSCOPE_END_USER
                                + ",tenant:" + mTenantId
                        );
                    } else {
                        toastShort("请输入有效的手机号码");
                    }

                }
            } else if (v.getId() == R.id.tv_getveritycode) {
                if (!isEmpty(et_acount)) {
                    if (MyUtil.isMobile(getText(et_acount))) {
                        getP().getVerityCode(getText(et_acount), Request_GetVerityCode.TYPE_REGISTER, Constant.LOGINSCOPE_END_USER);

                    } else {
                        toastShort("请输入有效的手机号码");
                    }

                }
            } else if (v.getId() == R.id.tv_pwdlogin) {
                startWithPop(PwdLoginFragment.newInstance(et_acount.getText().toString()));

            }else if (v.getId() == R.id.tv_jump_over){
                MyAppProvider.startMainActivty("", "");
                getActivity().finish();
            }else if (v.getId() == R.id.btn_registrationagreement){
                PdfViewerActivity.startSelf(getActivity(), "jinchang_platform_privacy.pdf", "隐私协议");
            }else if (v.getId() == R.id.btn_yinsiagreement){
                PdfViewerActivity.startSelf(getActivity(), "jinchang_platform_notice.pdf", "平台须知");
            }
        }
    }

    @Override
    public SMSLoginPresenter newP() {
        return new SMSLoginPresenter();
    }

    @Override
    public void showVerifyCode() {
        timer.start();
        tv_getveritycode.setTextColor(getRES_Color(R.color.text_gray_9));
        tv_getveritycode.setClickable(false);
    }

    @Override
    public void showLoginResule(UserInfoBean resule) {
        //toastShort("登录成功 "+resule);
        getP().refreshToken();
        //  start(SetPwdFragment.newInstance(getEditText(et_veritycode)));
    }

    @Override
    public void showLoginFaildMsg(String msg) {
        toastShort(msg);
    }

    @Override
    public void showRefreshTokenResult(boolean isSuccess) {
        if (isSuccess) {
            MyAppProvider.startMainActivty("", "");
            getActivity().finish();
        }
    }


    /**
     * 验证码倒计时
     */
    private CountDownTimer timer = new CountDownTimer(60 * 1000, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            if (tv_getveritycode != null) {
                tv_getveritycode.setText("重新获取(" + millisUntilFinished / 1000 + "秒)");
            }
        }

        @Override
        public void onFinish() {
            if (tv_getveritycode != null) {
                tv_getveritycode.setEnabled(true);
                tv_getveritycode.setText("重新获取");
                tv_getveritycode.setTextColor(getRES_Color(R.color.colorAccent));
                tv_getveritycode.setClickable(true);
            }
        }
    };

}
